/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fh.controller;
import fh.component.DataSource;
import fh.model.Location;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Bart
 */
public class LocationController {

    public boolean insertLocation(Location l) {
        String insertLocationQuery = "INSERT INTO `location` (name, address, city, max_capacity) values('"
                + l.getName() + "','"
                + l.getAddress() + "', '"
                + l.getCity() + "', "
                + l.getMax_capacity() + ")";

        DataSource.executeUpdateQuery(insertLocationQuery);
        return true;
    }

    public DefaultComboBoxModel getLocationsList() {
        try {
            DefaultComboBoxModel locations = new DefaultComboBoxModel();

            String query = "SELECT * FROM location";

            ResultSet rs = DataSource.executeSelectQuery(query);
            if (rs != null) {
                Location l;
                while (rs.next()) {
                    l = new Location();
                    l.setId(rs.getInt(1));
                    l.setName(rs.getString(2));
                    l.setAddress(rs.getString(3));
                    l.setCity(rs.getString(4));
                    l.setMax_capacity(rs.getInt(5));
                    locations.addElement(l);
                }

                return locations;
            } else {
                return null;
            }
        } catch (SQLException e) {
            System.out.println("Deze error:");
            System.out.println(e);
            return null;
        }
    }

    public Location getLocationById(int id) {
        try {
            Location l = new Location();

            String query = "SELECT * FROM location WHERE id=?";
            System.out.println(query);
            ResultSet rs = DataSource.executeSelectWithIdQuery(query, id);
            System.out.println();
            if (rs != null) {
                while (rs.next()) {
                    l.setId(rs.getInt(1));
                    l.setName(rs.getString(2));
                    l.setAddress(rs.getString(3));
                    l.setCity(rs.getString(4));
                    l.setMax_capacity(rs.getInt(5));

                }
                return l;
            } else {
                return null;
            }
        } catch (SQLException e) {
            System.out.println("Deze error:");
            System.out.println(e);
            return null;
        }
    }

    public int deleteLocationById(int id) {

        String query = "DELETE FROM location WHERE id=?";
        System.out.println(query);
        return DataSource.executeDeleteQuery(query, id);
    }

    public boolean updateLocation(Location l) {
        String updateEventQuery = "UPDATE `location` SET "
                + "`name`='" + l.getName() + "', "
                + "`address`='" + l.getAddress() + "', "
                + "`city`='" + l.getCity() + "', "
                + "`max_capacity`=" + l.getMax_capacity()
                + " WHERE `id`=" + l.getId();
        DataSource.executeUpdateQuery(updateEventQuery);
        return true;
    }
}
